package com.jielin.handling.controller.auditresult;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jielin.handling.Annotation.MyPower;
import com.jielin.handling.bean.AppResult;
import com.jielin.handling.entity.auditrecorder.AuditRecoder;
import com.jielin.handling.entity.auditresult.AuditResult;
import com.jielin.handling.entity.police.Cases;
import com.jielin.handling.entity.tsuspect.TSuspec;
import com.jielin.handling.entity.vo.AuditRecordVo;
import com.jielin.handling.entity.vo.AuditResultVo;
import com.jielin.handling.entity.vo.CaseAndSuspectVo;
import com.jielin.handling.entity.vo.CaseAuditVo;
import com.jielin.handling.page.DataGrid;
import com.jielin.handling.service.auditresult.AuditFailedService;
import com.jielin.handling.util.DateUtil;
import com.jielin.handling.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangjienice on 2019-04-01.
 */
@CrossOrigin
@RestController
@RequestMapping("/auditRuslt/")
@Slf4j
@MyPower(name = "卷宗审核和管理模块模块", url = "/auditRuslt")
public class AuditRusltController {
    @Autowired
    AuditFailedService service;

//
//    @RequestMapping("nameAndCode")
//    public AppResult nameAndCode() {
//        AppResult result = new AppResult();
//        JSONObject data = new JSONObject();
//        log.info("auditresult:{}", auditresult);
//        List<AuditResult> list = service.nameAndCode(auditresult);
//        if (list.size() > 0) {
//            data.put("value", list);
//            result.setCode(200);
//            result.setMsg("查询成功");
//            result.setData(data);
//        } else {
//            data.put("value", 0);
//            result.setCode(-1);
//            result.setMsg("查询为空");
//            result.setData(data);
//        }
//        return result;
//    }

    /**
     * 卷宗审核不通过（卷宗和案管公用接口）
     */
    @MyPower(name = "卷宗和案管审核不通过", url = "/auditFailed")
    @RequestMapping("auditFailed")
    public AppResult auditFailed(@RequestBody JSONObject queryCondition) {
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        log.info("queryCondition:{}", queryCondition);
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        AuditResult auditresult = new AuditResult();
        auditresult.setQueryCondition(queryCondition);
        auditresult.setPage(pageNum, pageSize);
        List<AuditResult> orderList = service.auditFailed(auditresult);

        PageInfo<AuditResult> pageInfo = new PageInfo<AuditResult>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;

    }

    /**
     * 审核已通过文件
     */
    @MyPower(name = "卷宗和案管审核已通过", url = "/throughAuditing")
    @RequestMapping(value = "throughAuditing")
    public AppResult throughAuditing(@RequestBody JSONObject queryCondition) {
        log.info("waitingCheck query...");
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        log.info("queryCondition:{}", queryCondition);
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        AuditResult auditresult = new AuditResult();
        auditresult.setQueryCondition(queryCondition);
        auditresult.setPage(pageNum, pageSize);
        List<AuditResult> orderList = service.throughAuditing(auditresult);

        PageInfo<AuditResult> pageInfo = new PageInfo<AuditResult>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;
    }

    /**
     * 待审核文件列表,法制员看所有案件信息需要办案单位handleunitcode参数，案管模块传参policeid--------
     */
    @MyPower(name = "待审核文件列表", url = "/waitCheck")
    @RequestMapping(value = "waitCheck")
    public AppResult waitCheck(@RequestBody JSONObject queryCondition) {
        log.info("Cases query...");
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        log.info("queryCondition:{}", queryCondition);
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        Cases casess = new Cases();
        casess.setQueryCondition(queryCondition);
        casess.setPage(pageNum, pageSize);
        List<Cases> orderList = service.waitCheck(casess);
        PageInfo<Cases> pageInfo = new PageInfo<Cases>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;
    }


    /**
     * 待审核文件嫌疑人下拉框
     */
    @MyPower(name = "待审核文件嫌疑人下拉框", url = "/waitCheckSuspectList" , isPublic = true)
    @RequestMapping(value = "waitCheckSuspectList")
        public AppResult waitCheckSuspectList(String unicode,String caseCode,String materialStatus) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        List<TSuspec> vo = service.waitCheckSuspectList(unicode,caseCode,materialStatus);
        data.put("suspect", vo);
        result.setCode(200);
        result.setMsg("待审核文件嫌疑人下拉框!");
        result.setData(data);

        return result;
    }

    /**
     * 卷宗信息列表（材料id已返回，用于根据id查看卷宗材料）
     */
    @MyPower(name = "卷宗信息列表", url = "/dossierInformation")
    @RequestMapping(value = "dossierInformation")
    public AppResult dossierInformation(@RequestBody JSONObject queryCondition) {
        log.info("Cases query...");
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        log.info("queryCondition:{}", queryCondition);
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        Cases casess = new Cases();
        casess.setQueryCondition(queryCondition);
        casess.setPage(pageNum, pageSize);
        List<Cases> orderList = service.dossierInformation(casess);

        PageInfo<Cases> pageInfo = new PageInfo<Cases>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;
    }

    /**
     * 卷宗状态改变
     *
     * @param
     */
    @MyPower(name = "卷宗状态改变", url = "/updateMaterial")
    @RequestMapping(value = "updateMaterial")
    public AppResult updateMaterial(@RequestBody AuditResult auditResult) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("auditResult:{}", JSONObject.toJSONString(auditResult));
        Integer value = service.updateMaterial(auditResult);
        if (value > 0) {
            data.put("value", value);
            result.setCode(200);
            result.setMsg("卷宗状态改变成功!");
            result.setData(data);
        } else {
            data.put("value", value);
            result.setCode(-1);
            result.setMsg("卷宗状态改变失败!");
            result.setData(data);
        }
        return result;
    }

    /**
     * 审核结果记录
     */
    @MyPower(name = "审核结果记录", url = "/insertAuditRecorde", isPublic = true) // TODO 新增接口
    @RequestMapping("insertAuditRecorde")
    public AppResult insertAuditRecorde(@RequestBody AuditResult auditResult) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("auditRecordVo:{}", JSONObject.toJSONString(auditResult));
        int value = service.insertAuditRecorde(auditResult);
        if (value > 0) {
            data.put("value", value);
            result.setCode(200);
            result.setMsg("审核结果记录成功!");
            result.setData(data);
        } else {
            data.put("value", value);
            result.setCode(-1);
            result.setMsg("审核结果记录失败!");
            result.setData(data);
        }
        return result;
    }

    /**
     * 根据民警id查询案件状态为已完结的案件信息
     *
     * @return
     */
    @MyPower(name = "根据民警id查询案件状态为已完结的案件信息", url = "/completeCase")
    @RequestMapping(value = "completeCase")
    public AppResult completeCase(@RequestBody JSONObject queryCondition) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("queryCondition:{}", JSONObject.toJSONString(queryCondition));
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        // 查询条件+分页
        Cases casess = new Cases();
        casess.setQueryCondition(queryCondition);
        casess.setPage(pageNum, pageSize);
        List<Cases> orderList = service.completeCase(casess);
        PageInfo<Cases> pageInfo = new PageInfo<Cases>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        data.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(data);
        return result;
    }

    /*
     * unicode 单位编号 统计分析卷宗出入库
     */
    @MyPower(name = "统计分析卷宗出入库", url = "/selectAccessLibrary")
    @RequestMapping("selectAccessLibrary")
    public AppResult selectAccessLibrary(String unicode) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("unicode:{}", JSONObject.toJSONString(unicode));
        CaseAuditVo vo = service.selectAccessLibrary(unicode);
        data.put("value", vo);
        result.setCode(200);
        result.setMsg("查询卷宗出入库统计成功!");
        result.setData(data);

        return result;
    }

    /**
     * 案管中心卷宗出入库详情列表
     *
     * @param queryCondition
     * @return
     */
    @MyPower(name = "卷宗出入库详情列表", url = "/selectAccessList", isPublic = true)
    @RequestMapping(value = "selectAccessList")
    public AppResult selectAccessList(@RequestBody JSONObject queryCondition) {
        log.info("Cases query...");
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        log.info("queryCondition:{}", queryCondition);
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        AuditResult auditResult = new AuditResult();
        auditResult.setQueryCondition(queryCondition);
        auditResult.setPage(pageNum, pageSize);
        List<AuditResult> orderList = service.selectAccessList(auditResult);

        PageInfo<AuditResult> pageInfo = new PageInfo<AuditResult>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;
    }

    /**
     * 根据案件查询卷宗出入库列表查看详情
     *
     * @param id 卷宗id
     * @return
     */
    @MyPower(name = "根据卷综id查询卷宗出入库详情", url = "/selectInAndOutAuditById")
    @RequestMapping(value = "selectInAndOutAuditById")
    public AppResult selectInAndOutAudit(String id, int pageSize, int pageNum) {
        log.info("Cases query...");
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        log.info("id:{}", id);
        List<AuditRecoder> orderList = service.selectInAndOutAuditById(id, pageSize, pageNum);

        PageInfo<AuditRecoder> pageInfo = new PageInfo<AuditRecoder>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;
    }

    /**
     * 卷宗入库
     *
     * @param jsonObject
     * @return
     */
    @MyPower(name = "卷宗入库", url = "/entryLibrary")
    @RequestMapping("entryLibrary")
    public AppResult entryLibrary(@RequestBody JSONObject jsonObject) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("jsonObject:{}", JSONObject.toJSONString(jsonObject));
        JSONArray array = jsonObject.getJSONArray("listArray");
        JSONObject json = null;
        AuditRecoder auditRecoder = jsonObject.getObject("auditRecoder", AuditRecoder.class);// 前台传的对象
        int M = 0;
        int J = 0;
        if (array != null && array.size() > 0) {
            for (int i = 0; i < array.size(); i++) {
                HashMap<String, String> map = new HashMap();
                json = (JSONObject) array.get(i);
                if (StringUtil.isEmpty((String) json.get("drawerCode")) || json.get("drawerCode") == null) {
                    data.put("value", 0);
                    result.setCode(200);
                    result.setMsg("柜号不能为空");
                    result.setData(data);
                    return result;
                }
                map.put("id", StringUtil.generatorUUID());
                map.put("name", json.get("name").toString());
                map.put("idValue", json.get("idValue").toString());
                map.put("drawerCode", json.get("drawerCode").toString());
                map.put("caseCode", auditRecoder.getCaseCode());
                map.put("caseName", auditRecoder.getCaseName());
                map.put("accessPoliceCode", auditRecoder.getAccessPoliceCode());
                map.put("accessPoliceName", auditRecoder.getAccessPoliceName());
                map.put("entryDate", DateUtil.formatDate(auditRecoder.getEntryDate(), DateUtil.DATE_FORMAT_MINUS));
                map.put("accessLibrary", "1");
                M = service.entryLibrary(map);
                J += M;
            }
        }
        if (J == array.size()) {
            data.put("J", J);
            result.setCode(200);
            result.setMsg("卷宗入库成功!");
            result.setData(data);
        } else {
            data.put("value", 0);
            result.setCode(-1);
            result.setMsg("一个或多个卷宗入库失败!");
            result.setData(data);
        }
        return result;

    }

    /**
     * 卷宗借阅
     *
     * @param jsonObject
     * @return
     */
    @MyPower(name = "卷宗借阅", url = "/borrowDossier")
    @RequestMapping("borrowDossier")
    public AppResult borrowDossier(@RequestBody JSONObject jsonObject) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("jsonObject:{}", JSONObject.toJSONString(jsonObject));
        JSONArray array = jsonObject.getJSONArray("listArray");
        JSONObject json = null;
        int M = 0;
        int J = 0;
        AuditRecoder auditRecoder = jsonObject.getObject("AuditRecoder", AuditRecoder.class);// 前台传的对象
        if (array != null && array.size() > 0) {
            for (int i = 0; i < array.size(); i++) {
                HashMap<String, String> map = new HashMap();
                json = (JSONObject) array.get(i);
                map.put("id", StringUtil.generatorUUID());
                map.put("name", json.get("name").toString());
                map.put("idValue", json.get("idValue").toString());
                if (StringUtil.isEmpty(json.get("borrowName").toString()) || json.get("borrowName") == null || StringUtil.isEmpty(json.get("borrowCode").toString())
                        || json.get("borrowCode") == null || StringUtil.isEmpty(json.get("borrowReason").toString()) || json.get("borrowReason") == null) {
                    data.put("value", 0);
                    result.setCode(200);
                    result.setMsg("民警或借阅原因不能为空");
                    result.setData(data);
                    return result;
                }
                map.put("borrowName", json.get("borrowName").toString());// 借阅民警
                map.put("borrowCode", json.get("borrowCode").toString());// 借阅民警编号
                System.out.println(json.getDate("borrowDate"));
                map.put("borrowDate", DateUtil.getFormatDate(json.getDate("borrowDate"), DateUtil.DATE_FORMAT_MINUS));
                map.put("borrowReason", json.get("borrowReason").toString());
                if (!StringUtil.isEmpty((String) json.get("causesAndTypes"))) {
                    map.put("causesAndTypes", json.get("causesAndTypes").toString());
                }
                map.put("caseCode", auditRecoder.getCaseCode());
                map.put("caseName", auditRecoder.getCaseName());
                map.put("outPoliceName", auditRecoder.getOutPoliceName().toString());// 出库经办民警
                map.put("outPoliceCode", auditRecoder.getOutPoliceCode().toString());// 出库经办警员编号
                map.put("accessLibrary", "2");
                M = service.borrowDossier(map);
                J += M;
            }
        }

        if (J == array.size()) {
            data.put("J", J);
            result.setCode(200);
            result.setMsg("卷宗借阅成功!");
            result.setData(data);
        } else {
            data.put("value", 0);
            result.setCode(-1);
            result.setMsg("卷宗借阅失败!");
            result.setData(data);
        }
        return result;
    }

    /**
     * 卷宗归还(入库)
     *
     * @param jsonObject
     * @return
     */
    @MyPower(name = "卷宗归还(入庫)", url = "/returnDossier", isPublic = true)
    @RequestMapping("returnDossier")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public AppResult returnDossier(@RequestBody JSONObject jsonObject) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("jsonObject:{}", JSONObject.toJSONString(jsonObject));
        JSONArray array = jsonObject.getJSONArray("listArray");// 取出前台传卷宗id和柜号
        JSONObject json = null;
        int J = 0;
        int M = 0;
        AuditRecoder auditRecoder = jsonObject.getObject("AuditRecoder", AuditRecoder.class);// 前台传的对象
        for (int i = 0; i < array.size(); i++) {
            HashMap<String, String> map = new HashMap();
            json = (JSONObject) array.get(i);
            if (json.get("drawerCode") == null || StringUtil.isEmpty((String) json.get("drawerCode"))) {
                data.put("value", 0);
                result.setCode(200);
                result.setMsg("柜号不能为空");
                result.setData(data);
                return result;
            }
            // map.put("id", StringUtil.generatorUUID());
            // map.put("auditId", json.get("auditId").toString());
            // String id =
            // service.selectRecoderId(auditRecoder.getCaseCode());//查询记录表中为借阅状态的那条记录的id
            map.put("id", json.get("id").toString());
            map.put("name", json.get("name").toString());
            map.put("idValue", json.get("idValue").toString());
            map.put("entryDate", DateUtil.formatDate(json.getDate("entryDate"), DateUtil.DATE_FORMAT_MINUS));
            map.put("drawerCode", json.get("drawerCode").toString());
            map.put("caseName", auditRecoder.getCaseName());
            map.put("caseCode", auditRecoder.getCaseCode());
            map.put("returnPoliceCode", auditRecoder.getReturnPoliceName());
            map.put("returnPoliceName", auditRecoder.getReturnPoliceName());
            map.put("accessLibrary", "3");
            J = service.returnDossier(map);
            M += J;
        }
        if (M == array.size()) {
            data.put("M", M);
            result.setCode(200);
            result.setMsg("卷宗归还成功!");
            result.setData(data);
        } else {
            data.put("value", 0);
            result.setCode(-1);
            result.setMsg("卷宗归还失败!");
            result.setData(data);
        }
        return result;
    }

    /**
     * 卷宗移交
     *
     * @param jsonObject
     * @return
     */
    @MyPower(name = "卷宗移交", url = "/transferDossier")
    @RequestMapping("transferDossier")
    public AppResult transferDossier(@RequestBody JSONObject jsonObject) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("jsonObject:{}", JSONObject.toJSONString(jsonObject));
        JSONArray array = jsonObject.getJSONArray("listArray");// 取出前台传卷宗id和柜号
        JSONObject json = null;
        int value = 0;
        AuditRecoder auditRecoder = jsonObject.getObject("AuditRecoder", AuditRecoder.class);// 前台传的对象
        if (array != null && array.size() > 0) {
            for (int i = 0; i < array.size(); i++) {
                HashMap<String, String> map = new HashMap();
                json = (JSONObject) array.get(i);
                if (StringUtil.isEmpty((String) json.get("transfeReason")) || json.get("transfeReason") == null || StringUtil.isEmpty((String) json.get("transferName"))
                        || json.get("transferName") == null || StringUtil.isEmpty((String) json.get("transferCode")) || json.get("transferCode") == null) {
                    data.put("value", 0);
                    result.setCode(200);
                    result.setMsg("移交原因或民警不能为空");
                    result.setData(data);
                    return result;
                }
            }
        }
        value = service.transferDossier(jsonObject);
        if (value == array.size()) {
            data.put("value", value);
            result.setCode(200);
            result.setMsg("卷宗移交成功!");
            result.setData(data);
        } else {
            data.put("value", 0);
            result.setCode(200);
            result.setMsg("卷宗失败!");
            result.setData(data);
        }
        return result;
    }

    /**
     * 移交列表
     *
     * @param queryCondition
     * @return
     */
    @MyPower(name = "移交列表", url = "/transferList", isPublic = true)
    @RequestMapping(value = "transferList", method = RequestMethod.POST)
    public AppResult transferList(@RequestBody JSONObject queryCondition) {
        log.info("Cases query...");
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        log.info("queryCondition:{}", queryCondition);
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        AuditRecoder auditRecoder = new AuditRecoder();
        auditRecoder.setQueryCondition(queryCondition);
        auditRecoder.setPage(pageNum, pageSize);
        List<AuditRecoder> orderList = service.transferList(auditRecoder);

        PageInfo<AuditRecoder> pageInfo = new PageInfo<AuditRecoder>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;
    }

    /**
     * 卷宗新增(登记下一步,未关联人员绑定案件,二次审讯登记下一步是新增)
     *
     * @param auditResult
     * @return
     */

    @MyPower(name = "卷宗新增", url = "/insertAudit")
    @RequestMapping("insertAudit")
    public AppResult insertAudit(@RequestBody AuditResult auditResult) {
        AppResult result = new AppResult();
        JSONObject
                data = new JSONObject();
        log.info("auditResult:{}", JSONObject.toJSONString(auditResult));
        int value = service.insertAudit(auditResult);
        if (value > 0) {
            data.put("value", value);
            result.setCode(200);
            result.setMsg("卷宗新增成功!");
            result.setData(data);
        } else {
            data.put("value", 0);
            result.setCode(-1);
            result.setMsg("卷宗新增失败!");
            result.setData(data);
        }
        return result;
    }

    /**
     * 卷宗撤回状态改变(待审核撤回)
     *
     * @param id            电子卷宗审核结果表的id
     * @param caseCode      警综案件编号
     * @param policeId      警员id
     * @param caseSuspectId 案件关联的嫌疑人id
     * @return
     */
    @MyPower(name = "卷宗撤回状态改变", url = "/updataWithdraw", isPublic = true) // TODO 新增接口
    @RequestMapping("updataWithdraw")
    public AppResult updataWithdraw(String id, String caseCode, String policeId, String caseSuspectId) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("id:{}", JSONObject.toJSONString(id));
        int value = service.updataWithdraw(id, caseCode, policeId, caseSuspectId);
        if (value > 0) {
            data.put("value", value);
            result.setCode(200);
            result.setMsg("卷宗撤回状态改变成功!");
            result.setData(data);
        } else {
            data.put("value", value);
            result.setCode(-1);
            result.setMsg("卷宗撤回状态改变失败!");
            result.setData(data);
        }
        return result;
    }

    /**
     * 统计该单位民警材料通过率（案管中心）
     *
     * @param unitCode 单位编号
     * @return
     */
    @MyPower(name = "统计该单位民警材料通过率", url = "/selectMaterialPassRate", isPublic = true)
    @RequestMapping("selectMaterialPassRate")
    public AppResult selectMaterialPassRate(String unitCode) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        String value = service.selectMaterialPassRate(unitCode);
        data.put("value", value);
        result.setCode(200);
        result.setMsg("统计该单位民警材料通过率成功!");
        result.setData(data);
        return result;
    }

    /**
     * 统计该单位民警材料通过率列表（案管中心）
     *
     * @param unitCode   单位编号
     * @param policeCode 民警警号(非必填)
     * @param policeName 民警姓名(非必填)
     * @param pageNum    第几页(非必填)
     * @param pageSize   多少行(非必填)
     * @return
     */
    @MyPower(name = " 统计该单位民警材料通过率列表", url = "/selectMaterialGroupByPolice", isPublic = true)
    @RequestMapping("selectMaterialGroupByPolice")
    public AppResult selectMaterialGroupByPolice(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") int pageSize, String unitCode, String policeCode,
                                                 String policeName) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        Page<AuditResultVo> startPage = PageHelper.startPage(pageNum, pageSize);// 使用分页插件方法
        service.selectMaterialGroupByPolice(unitCode, policeCode, policeName);
        // 封装数据
        DataGrid dataGridResultInfo = new DataGrid();
        dataGridResultInfo.setRows(startPage.getResult());// 当前查询的数据
        dataGridResultInfo.setTotal((int) startPage.getTotal());// 总页数
        data.put("value", dataGridResultInfo);
        result.setCode(200);
        result.setMsg("统计该单位民警材料通过率列表成功!");
        result.setData(data);
        return result;
    }

    /**
     * @param queryCondition 根据案件编号查询案件关联所有卷宗信息(以人为单位)
     * @return
     */

    @MyPower(name = "根据案件编号查询案件关联所有卷宗信息", url = "/selectMaterialByCaseCode", isPublic = true)
    @RequestMapping("selectMaterialByCaseCode")
    public AppResult selectMaterialByCaseCode(@RequestBody JSONObject queryCondition) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        log.info("queryCondition:{}", JSONObject.toJSONString(queryCondition));
        AuditResult auditResult = new AuditResult();
        auditResult.setQueryCondition(queryCondition);
        List<AuditResult> list = service.selectMaterialByCaseCode(auditResult);
        data.put("list", list);
        result.setCode(200);
        result.setMsg("根据案件编号查询案件关联所有卷宗信息成功!");
        result.setData(data);
        return result;
    }

    /**
     * 根据案件编号查询案件关联的卷宗状态为4种状态公用卷宗信息
     *
     * @param auditResult
     * @return
     */
    @MyPower(name = "根据案件编号查询案件关联的卷宗状态为4种状态公用卷宗信息", url = "/selectMaterialByAccessLibrary", isPublic = true)
    @RequestMapping("selectMaterialByAccessLibrary")
    public AppResult selectMaterialByAccessLibrary(@RequestBody AuditResult auditResult) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        List<AuditResult> list = service.selectMaterialByAccessLibrary(auditResult);
        data.put("list", list);
        result.setCode(200);
        result.setMsg("根据案件编号查询案件关联所有卷宗信息成功!");
        result.setData(data);
        return result;
    }

    /**
     * 根据案件编号查询案件关联的卷宗状态为4种状态的人员下拉框
     *
     * @param
     * @return
     */
    @MyPower(name = "根据案件编号查询案件关联的卷宗状态为4种状态的人员下拉框", url = "/selectSuectByAccessLibrary", isPublic = true)
    @RequestMapping("selectSuectByAccessLibrary")
    public AppResult selectSuectByAccessLibrary(String name, String caseCode) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        List<AuditResult> list = service.selectSuectByAccessLibrary(name, caseCode);
        data.put("list", list);
        result.setCode(200);
        result.setMsg("根据案件编号查询案件关联的卷宗状态为4种状态的人员下拉框成功!");
        result.setData(data);
        return result;
    }

    /**
     * 根据案件编号获取该案件下面的卷宗,嫌疑人姓名,登记时间下拉框(提交审核等四大操作)
     *
     * @param caseCode
     * @param materialStatus
     * @return
     */

    @MyPower(name = "根据案件编号获取该案件下面的卷宗,嫌疑人姓名,登记时间下拉框", url = "/selectDosserList", isPublic = true)
    @RequestMapping("selectDosserList")
    public AppResult selectDosserList(String caseCode, String materialStatus, String policeId, String handleUnitCode) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        if (StringUtil.isEmpty(caseCode)) {
            result.setCode(-1);
            result.setMsg("根据案件编号获取该案件下面的卷宗,嫌疑人姓名,登记时间失败！");
            result.setData(data);
        }
        List<AuditResult> list = service.selectDosserList(caseCode, materialStatus, policeId, handleUnitCode);
        if (list.size() > 0) {
            data.put("list", list);
            result.setCode(200);
            result.setMsg("根据案件编号获取该案件下面的卷宗,嫌疑人姓名,登记时间成功！");
            result.setData(data);
        }
        return result;
    }

    @MyPower(name = "根据案件编号获取该案件下面的嫌疑人姓名下拉框", url = "/selectSuspectDosserList", isPublic = true)
    @RequestMapping("selectSuspectDosserList")
    public AppResult selectSuspectDosserList(String caseCode, String materialStatus, String policeId) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        if (StringUtil.isEmpty(caseCode)) {
            result.setCode(-1);
            result.setMsg("根据案件编号获取该案件下面的嫌疑人姓名下拉框失败！");
            result.setData(data);
        }
        List<AuditResult> list = service.selectSuspectDosserList(caseCode,materialStatus, policeId);
        if (list.size() > 0) {
            data.put("list", list);
            result.setCode(200);
            result.setMsg("根据案件编号获取该案件下面的嫌疑人姓名成功！");
            result.setData(data);
        }
        return result;
    }

    /**
     * 根据案件编号和嫌疑人身份证号查询卷宗出入库的案件卷宗信息详情信息(卷宗出入库案卷卷宗信息)
     *
     * @param caseCode
     * @param idValue
     * @return
     */
    @MyPower(name = "根据案件编号和嫌疑人身份证号查询卷宗出入库的案件卷宗信息详情信息", url = "/selectCaseAndSuspect", isPublic = true)
    @RequestMapping("selectCaseAndSuspect")
    public AppResult selectCaseAndSuspect(String caseCode, String idValue) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        Map map = service.selectCaseAndSuspect(caseCode, idValue);
        data.put("map", map);
        result.setCode(200);
        result.setMsg("根据案件编号和嫌疑人身份证号查询卷宗和案件的详情信息成功！");
        result.setData(data);
        return result;
    }

    /**
     * 卷宗出入库>>>>>>>>>>卷宗出入库详情列表
     *
     * @param queryCondition
     * @return
     */
    @MyPower(name = "根据嫌疑人身份证号获取嫌疑人卷宗出入库详情列表", url = "/selectInAndOut", isPublic = true)
    @RequestMapping(value = "selectInAndOut", method = RequestMethod.POST)
    public AppResult selectInAndOut(@RequestBody JSONObject queryCondition) {
        log.info("Cases query...");
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        log.info("queryCondition:{}", queryCondition);
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        AuditRecoder auditRecoder = new AuditRecoder();
        auditRecoder.setQueryCondition(queryCondition);
        auditRecoder.setPage(pageNum, pageSize);
        List<AuditRecoder> orderList = service.selectInAndOut(auditRecoder);

        PageInfo<AuditRecoder> pageInfo = new PageInfo<AuditRecoder>(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;
    }

    /**
     * 根据案件编号查询案件下所有的卷宗信息(以案件为单位)
     *
     * @param caseCode
     * @return
     */

    @MyPower(name = "根据案件编号查询案件下所有的卷宗信息(以案件为单位)", url = "/selectByCaseCode", isPublic = true)
    @RequestMapping("selectByCaseCode")
    public AppResult selectCaseCode(String caseCode) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        List<AuditResult> list = service.selectByCaseCode(caseCode);
        data.put("list", list);
        result.setCode(200);
        result.setMsg("根据案件编号查询案件下所有的卷宗信息(以案件为单位)成功！");
        result.setData(data);
        return result;
    }

    /**
     * 根据案件编号查询案件下所有的卷宗信息(以案件为单位)
     *
     * @param
     * @return
     */

    @MyPower(name = "根据单位编号查询单位下所有的卷宗信息(以单位为单位)", url = "/selectAllAuditByUnitCode", isPublic = true)
    @RequestMapping("selectAllAuditByUnitCode")
    public AppResult selectAllAuditByUnitCode(@RequestBody JSONObject queryCondition) {
        log.info("Cases query...");
        AppResult result = new AppResult();
        JSONObject date = new JSONObject();
        log.info("queryCondition:{}", queryCondition);
        int pageNum = queryCondition.getIntValue("pageNum");
        int pageSize = queryCondition.getIntValue("pageSize");
        AuditRecoder auditRecoder = new AuditRecoder();
        auditRecoder.setQueryCondition(queryCondition);
        auditRecoder.setPage(pageNum, pageSize);
        List<AuditResult> orderList = service.selectAllAuditByUnitCode(auditRecoder);
        PageInfo<AuditResult> pageInfo = new PageInfo(orderList);
        DataGrid dataGrid = new DataGrid(pageInfo.getTotal(), pageInfo.getList());
        date.put("datagrid", dataGrid);
        result.setCode(200);
        result.setMsg("query success");
        result.setData(date);
        return result;
    }

    /**
     * 根据id查询卷宗信息
     *
     * @param id 卷宗id
     * @return
     */
    @MyPower(name = "查询卷宗状态信息", url = "/selectById", isPublic = true)
    @RequestMapping("selectById")
    public AppResult selectById(String id) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        AuditResult list = service.selectById(id);
        data.put("list", list);
        result.setCode(200);
        result.setMsg("查询卷宗信息！");
        result.setData(data);
        return result;
    }

    /**
     * 根据id查询卷宗信息
     *
     * @param id 卷宗id
     * @return
     */
    @MyPower(name = "查询卷宗状态信息及嫌疑人列表", url = "/selectAuditResultSuspectById", isPublic = true)
    @RequestMapping("selectAuditResultSuspectById")
    public AppResult selectAuditResultSuspectById(String id,String caseCode, String materialStatus,String policeId) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        if (StringUtil.isEmpty(id)||StringUtil.isEmpty(caseCode)||StringUtil.isEmpty(policeId)) {
            result.setCode(-1);
            result.setMsg("参数为空，查询卷宗状态信息及嫌疑人列表失败！");
            result.setData(data);
        }
        Map<String, Object> map = new HashMap();
        AuditResult list = service.selectById(id);
        List<AuditResult> suspectList = service.selectSuspectDosserList(caseCode,materialStatus, policeId);
        map.put("caseResultList",list);
        map.put("suspectList",suspectList);
        data.put("map", map);
        result.setCode(200);
        result.setMsg("查询卷宗信息！");
        result.setData(data);
        return result;
    }

    /**
     * 根据单位编号查询案管柜里面卷宗所对应的案件
     *
     * @param handleUnitCode 单位编号
     * @return
     */
    @MyPower(name = "根据单位编号查询案管柜里面卷宗所对应的案件", url = "/selectCasecodeInCabin", isPublic = true)
    @RequestMapping("selectCasecodeInCabin")
    public AppResult selectCasecodeInCabin(String handleUnitCode, String drawerCode) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        List<AuditResult> list = service.selectCasecodeInCabin(handleUnitCode, drawerCode);
        data.put("list", list);
        result.setCode(200);
        result.setMsg("查询管柜里面卷宗所对应的案件！");
        result.setData(data);
        return result;
    }

    /**
     * 根据单位编号查询所有案管柜的状态
     *
     * @param handleUnitCode 单位编号
     * @return
     */
    @MyPower(name = "根据单位编号查询所有案管柜的状态", url = "/selectStateInCabin", isPublic = true)
    @RequestMapping("selectStateInCabin")
    public AppResult selectStateInCabin(String handleUnitCode) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        Map map = service.selectStateInCabin(handleUnitCode);
        data.put("map", map);
        result.setCode(200);
        result.setMsg("查询所有案管柜!");
        result.setData(data);
        return result;
    }

    /**
     * 根据单位编号查询柜子未使用和使用的个数
     *
     * @param handleUnitCode 单位编号
     * @return
     */
    @MyPower(name = "根据单位编号查询柜子未使用和使用的个数", url = "/selectCountInCabin", isPublic = true)
    @RequestMapping("selectCountInCabin")
    public AppResult selectCountInCabin(String handleUnitCode) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        CaseAndSuspectVo vo = service.selectCountInCabin(handleUnitCode);
        data.put("vo", vo);
        result.setCode(200);
        result.setMsg("查询柜子未使用和使用的个数！");
        result.setData(data);
        return result;
    }

    /**
     * 根据单位编号查询柜子号码
     *
     * @param handleUnitCode 单位编号
     * @return
     */
    @MyPower(name = "根据单位编号查询柜子号码", url = "/selectCabinetNumber", isPublic = true)
    @RequestMapping("selectCabinetNumber")
    public AppResult selectCabinetNumber(String id) {
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        List<CaseAndSuspectVo> list = service.selectCabinetNumber(id);
        data.put("list", list);
        result.setCode(200);
        result.setMsg("查询柜子号码！");
        result.setData(data);
        return result;
    }

}
